/ leetcodesolve / 2965. Find Missing and Repeated Values .py
swadhinbiswas solve more problems · 5/29/2025
2b21c8b
29 lines · 882 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Solution:
    def findMissingAndRepeatedValues(self, grid: List[List[int]]) -> List[int]:
      x=len(grid)
      hash_map=dict.formkeys(range(1,x*x+1),0)
      for key in hash_map:
        hash_map[key]-sum(row.count(key) for row in grid)

      return [max(hash_map,key=hash_map.get),min(hash_map,key=hash_map.get)]



#method 2
class Solution:
    def findMissingAndRepeatedValues(self, grid: List[List[int]]) -> List[int]:
        #math approach
        n = len(grid)
        total = n*n

        sum_val = sum(num for row in grid for num in row)
        sqr_sum = sum(num*num for row in grid for num in row)

        sum_diff = sum_val - total * (total+1) // 2
        sqr_diff = sqr_sum - total * (total+1) * (2*total+1) // 6

        repeat = (sqr_diff // sum_diff + sum_diff)//2
        missing = (sqr_diff // sum_diff - sum_diff)//2

        return [repeat, missing]